{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1                       # 整型\n",
    "b = 3.14                    # 浮点型\n",
    "c = 'abc'                   # 字符串\n",
    "e = b'abc'                  # 字节流，即字符串的二进制表示\n",
    "f = [1, 2, 3]               # 列表\n",
    "g = ('Python', 'Java', 'C') # 元组，相当于不能修改的列表\n",
    "h = {\"Alice\": \"Girl\"}       # 字典\n",
    "i = {'apple', 'orange', 'pear'} # 集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "大于零\n"
     ]
    }
   ],
   "source": [
    "# 条件判断\n",
    "x = 1\n",
    "if x > 0:\n",
    "    print(\"大于零\")\n",
    "elif x == 0:\n",
    "    print(\"等于零\")\n",
    "else:\n",
    "    print(\"小于零\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 循环\n",
    "for i in [1, 2, 3]:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n这是\\n多行\\n注释\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这是单行注释\n",
    "\n",
    "'''\n",
    "这是\n",
    "多行\n",
    "注释\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义计算正方形面积函数\n",
    "\n",
    "def area(x):\n",
    "    return x * x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求最大值\n",
    "\n",
    "max(1, 3, 0, -2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求绝对值\n",
    "\n",
    "abs(-26)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "division by zero\n"
     ]
    }
   ],
   "source": [
    "# 捕获1除以0的异常并打印错误信息\n",
    "\n",
    "try:\n",
    "    1/0\n",
    "except Exception as e:\n",
    "    print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义一个人（People)类，包含姓名（name）和年龄(age)\n",
    "\n",
    "class People:\n",
    "    \n",
    "    def __init__(self, name, age):\n",
    "        \"\"\"\n",
    "        初始化方法，将参数赋值给类成员变量\n",
    "        \"\"\"\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        \n",
    "    def speak(self):\n",
    "        \"\"\"\n",
    "        打印成员变量\n",
    "        \"\"\"\n",
    "        print(\"我叫%s, 我今年%s岁。\" % (self.name, self.age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我叫张三, 我今年33岁。\n"
     ]
    }
   ],
   "source": [
    "# 实例化一个类的实例对象并调用它的方法\n",
    "\n",
    "p = People('张三', '33')\n",
    "p.speak()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "my_module  \n",
    "├── __init__.py\n",
    "├── abc.py\n",
    "└── xyz.py\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
